/**
 * 
 */
package com.hk.Mgr.web.controller;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.cooperation.CompanyReq;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.HqlFilter;
import com.hk.commons.StringHelper;
import com.hk.commons.entity.UserChannel;
import com.hk.commons.entity.product.Company;
import com.hk.service.IUserChannel;
import com.hk.service.product.ICompany;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author psx 产品信息实现类
 */
@Controller
@RequestMapping(value = BaseCtrl.MgrRootPath + "/userChannelCtrl")
public class UserChannelCtrl extends BaseCtrl {

	@Autowired
	private IUserChannel userChannelService;

	/**
	 *
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public ResponseBase<List<UserChannel>> getList(
			@RequestBody CompanyReq req) {
		ResponseBase<List<UserChannel>> resp = new ResponseBase<List<UserChannel>>();

		HqlFilter hqlFilter = new HqlFilter();

		List<UserChannel> result = userChannelService.findByFilter(hqlFilter, req.getCurPage(), req.getPageSize());

		List<UserChannel> totalList = userChannelService.findByFilter(hqlFilter);

		resp.setSize(totalList.size());
		resp.setResultCode(StatusCode.SUCCESS.key());
		resp.setResult(result);

		return resp;
	}

	/**
	 * @Title: saveProductInfo
	 * @Description: TODO
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ResponseBase<Boolean> save(
			@RequestBody UserChannel u) {
		logger.info("userChannelCtrl will add new userChannel channelId = {}, name={}", u.getChannelId(),u.getName());
		ResponseBase<Boolean> resp = new ResponseBase<>();
		resp.setResult(false);

		String name = u.getName();
		if(StringHelper.isEmpty(name) || StringHelper.isEmpty(u.getChannelDesc()) || u.getChannelId() == null){
			resp.setMsg("参数错误，渠道名称或注释为空！");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			return resp;
		}


		if(u.getUserRoleId() == null){
			resp.setMsg("参数错误，渠道所属角色为空！");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			return resp;
		}

		if(userChannelService.beforeAddCheck(name, u.getChannelId()) == false){
			resp.setMsg("参数错误，该渠道名称已存在！");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			return resp;
		}

		Boolean result = userChannelService.add(u);

		if (result){
			resp.setResult(true);
			resp.setMsg("添加渠道成功！");
			resp.setResultCode(StatusCode.EXCEPTION.key());

		}else{
			resp.setMsg("添加失败！");
			resp.setResultCode(StatusCode.SUCCESS.key());
		}
		return resp;
	}
	
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseBase<Boolean> update(
			@RequestBody UserChannel u) {
		ResponseBase<Boolean> resp = new ResponseBase<>();
		resp.setResult(false);

		Integer id = u.getId();
		if(id == null || userChannelService.findById(id) == null || u.getChannelId() == null){
			resp.setMsg("参数错误，该渠道不存在，此次更新失败！");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			return resp;
		}

		String name = u.getName();

		if(StringHelper.isEmpty(name) || StringHelper.isEmpty(u.getChannelDesc())){
			resp.setMsg("参数错误，渠道名称或注释为空！");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			return resp;
		}

		if(u.getUserRoleId() == null){
			resp.setMsg("参数错误，渠道所属角色为空！");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			return resp;
		}

		if(userChannelService.beforeAddCheck(name, u.getChannelId()) == false){
			resp.setMsg("参数错误，该渠道名称已存在！");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			return resp;
		}
		userChannelService.update(u);

		resp.setResult(true);
		resp.setMsg("更改渠道成功！");
		resp.setResultCode(StatusCode.SUCCESS.key());
		return resp;
	}

	 
}
